package com.wanaofu.model;

import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;

import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name = "WH_BATCH")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "OP_TYPE", length = 10 ,discriminatorType = DiscriminatorType.STRING)
public abstract class AbstractWarehouseBatch extends AbstractBaseEntity{
	
	@Column(name = "OP_DATE", nullable=false)
	@NotNull
    @Past
	protected Date opDate ;
    
	@ManyToOne(cascade = CascadeType.ALL)
	@ForeignKey(name="FK_BATCH_USER_ID")
	@JoinColumn(name="user_id")
	@Valid
	@NotNull(message="操作用户"+"{javax.validation.constraints.NotNull.message}")
	protected UserInfo user;
    
	@Column(name = "REMARK", nullable=false ,length=255)
    @Size(min=0,max=120)
	protected String remark;
	
    @Column(name = "BATCH_CODE", nullable=false)
    @NotNull
    @Size(min=0,max=60)
	protected String batchCode ;
    
    @OneToMany(mappedBy = "batch", cascade = { CascadeType.ALL })  
    @OrderBy("product ASC")
    @Size(min=1,max=200)
    @NotNull
    @Valid
    protected List<WarehouseBatchItem> items = new ArrayList<WarehouseBatchItem>();
    
    /**
     *  how this batch affect the balance, 
     *  @return : 
     *   -1 reduce the balance,
     *   1  add the balance
     */
    public abstract Integer affectBalceAmount(Integer amt) ;
    
	@Override
	boolean businessEquals(Object p) {
		// TODO Auto-generated method stub
		return false;
	}
	
    /**
     * 
     */
    public List<WarehouseBatchItem> getDistinctItems() {
    	List<WarehouseBatchItem> list =  new ArrayList<WarehouseBatchItem>();
    	for(WarehouseBatchItem item : this.items) {
    		boolean find = false ;
    		for(WarehouseBatchItem i : list) {
    			if (item.getProduct().equals(i.getProduct())
    				&& item.getPosition().equals(i.getPosition())) {
    				i.setAmount(i.getAmount() + item.getAmount()) ;
    				find = true ;
    				break ;
    			}
    		}
    		if (!find)
    		 list.add(new WarehouseBatchItem(item.getProduct(),item.getAmount(),item.getPosition(),null));
    	}
    	return list ;
    }
    public Integer getTotalAmount() {
    	Integer total = 0 ;
    	for(WarehouseBatchItem item : this.items) {
    		total = total + item.getAmount() ;
    	}
    	return total ;
    } 
    
    public String getBatchNo() {
    	Calendar cal= Calendar.getInstance() ;
    	cal.setTime(this.opDate) ;
    	return String.valueOf(cal.get(Calendar.YEAR)) + String.valueOf(cal.get(Calendar.MONTH)+101).substring(1) ;
    }
    
	public Date getOpDate() {
		return opDate;
	}

	public void setOpDate(Date oDate) {
		this.opDate = oDate;
	}

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo oUser) {
		this.user = oUser;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getBatchCode() {
		return batchCode;
	}

	public void setBatchCode(String batchCode) {
		this.batchCode = batchCode;
	}

	public List<WarehouseBatchItem> getItems() {
		return items;
	}

	public void setItems(List<WarehouseBatchItem> items) {
		this.items = items;
	}
	
}
